package com.csair.common.utils;

import com.csair.common.AppUtilsConfig;

import java.util.Map;

/**
 * @author vaycent (vaycentsun@gmail.com)
 * @date 2018/6/8
 */

public class FeaturesSwitchUtil {
    private static final String FEATURE = "FEATURE_";
    private static final String FEATURE_VALUE = "FEATURE_VALUE_";


    public static void save(String featureName, boolean switchOn) {
        SharedPreferencesUtil.getInstance(AppUtilsConfig.PREFERENCE_FEATURESSWITCH_FILENAME).saveBoolean(FEATURE + featureName,switchOn);
    }

    public static boolean isFeatureOn(String featureName) {
        return SharedPreferencesUtil.getInstance(AppUtilsConfig.PREFERENCE_FEATURESSWITCH_FILENAME).getBoolean(FEATURE + featureName, false);
    }

    public static void saveFeatureValue(String featureName, String value) {
        SharedPreferencesUtil.getInstance(AppUtilsConfig.PREFERENCE_FEATURESSWITCH_FILENAME).saveString(FEATURE_VALUE + featureName, value);
    }

    public static String getFeatureValue(String featureName) {
        return SharedPreferencesUtil.getInstance(AppUtilsConfig.PREFERENCE_FEATURESSWITCH_FILENAME).getString(FEATURE_VALUE + featureName, "");
    }

    public static void cleanAllFeaturesSwitch() {
        Map<String, ?> map =SharedPreferencesUtil.getInstance(AppUtilsConfig.PREFERENCE_FEATURESSWITCH_FILENAME).getMapFromSp();
        for (String key : map.keySet()) {
            if (key != null && key.startsWith(FEATURE_VALUE))
                saveFeatureValue(key.substring(FEATURE_VALUE.length()), "");
            else if (key != null && key.startsWith(FEATURE))
                save(key.substring(FEATURE.length()), false);
        }
    }
}
